class Sphere extends BaseShape{
 float angle = 0;
 float radius = 1;
 float latheAngle = 0;
 float latheRadius = 0;
int pts = 40;
int segments = 60;
PVector vertexA = null;
PVector vertexB = null;
PVector vertexC = null;
PVector vertexD = null;
int verticesPlaced = 0;
PVector vertices[],vertices2[];
 
 
 public Sphere(float radius)
 {
  this.radius = radius; 
 }
 
 public Sphere()
 {
  this(1); 
 }
 
 void draw() {
   PVector vertices[] = new PVector[pts+1];
   PVector vertices2[] = new PVector[pts+1];
   angle = 0;
   for(int i=0; i<=pts; i++){
      vertices[i] = new PVector();
      vertices2[i] = new PVector();
      vertices[i].x = sin(radians(angle))*radius;
      vertices[i].z = cos(radians(angle))*radius;
      angle+=360.0/pts;
    }
    
  latheAngle = 0;
  verticesPlaced = 0;
  vertexA = null;
  vertexB = null;
  vertexC = null;
  vertexD = null;
  for(int i=0; i<=segments; i++){
    beginShape(QUADS);
    for(int j=0; j<=pts; j++){
      if (i>0){
        //vertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
        placeVertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
      }
      vertices2[j].x = cos(radians(latheAngle))*vertices[j].x;
      vertices2[j].y = sin(radians(latheAngle))*vertices[j].x;
      vertices2[j].z = vertices[j].z;
      //vertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
      placeVertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
    }
      latheAngle+=360.0/segments;
    endShape();
  }
 }
 
 void placeVertex(float x, float y, float z){
   verticesPlaced++;
   if(vertexD!=null)
   {
    if(vertexC!=null) 
    {
     if(vertexB!=null)
     {
      vertexA = vertexB;
     }
     vertexB = vertexC; 
    }
    vertexC = vertexD;
   }
   vertexD = new PVector(x,y,z);
   
   if(verticesPlaced>=4 && verticesPlaced%2==0){
     vertex(vertexA.x, vertexA.y, vertexA.z);
     vertex(vertexB.x, vertexB.y, vertexB.z);     
     vertex(vertexD.x, vertexD.y, vertexD.z);
     vertex(vertexC.x, vertexC.y, vertexC.z);

      if(vertexB.x==vertexA.x && vertexB.y==vertexA.y && vertexB.z ==vertexA.z){
       PVector vector1 = PVector.sub(vertexC, vertexA);
       PVector vector2 = PVector.sub(vertexD, vertexA);
       PVector vectorN = vector2.cross(vector1);
       vectorN.normalize();
       normal(vectorN.x, vectorN.y, vectorN.z); 
      } 
      else 
      {
       PVector vector1 = PVector.sub(vertexB, vertexA);
       PVector vector2 = PVector.sub(vertexD, vertexA);
       PVector vectorN = vector2.cross(vector1);
       vectorN.normalize();
       normal(vectorN.x, vectorN.y, vectorN.z);
      }
      /*
      if(vertexB.x==vertexA.x && vertexB.y==vertexA.y && vertexB.z ==vertexA.z){
       println(verticesPlaced);
       println("Vectors A and B are the same"); 
      }
      if(vertexB.x==vertexC.x && vertexB.y==vertexC.y && vertexB.z ==vertexC.z){
       println(verticesPlaced);
       println("Vectors B and C are the same"); 
      }
      if(vertexC.x==vertexD.x && vertexC.y==vertexD.y && vertexC.z ==vertexD.z){
       println(verticesPlaced);
        println("Vectors C and D are the same"); 
      }
      if(vertexC.x==vertexA.x && vertexC.y==vertexA.y && vertexC.z ==vertexA.z){
       println(verticesPlaced);
        println("Vectors A and C are the same"); 
      }
      if(vertexA.x==vertexD.x && vertexA.y==vertexD.y && vertexA.z ==vertexD.z){
       println(verticesPlaced);
        println("Vectors D and A are the same"); 
      }
      if(vertexB.x==vertexD.x && vertexB.y==vertexD.y && vertexB.z ==vertexD.z){
       println(verticesPlaced);
        println("Vectors D and B are the same"); 
      }*/
      
   }
   
   
 }
}
